<?php
require_once ("db_connection.php");
include "ImageResizer.php";
session_start();
$session_id='1'; //$session id
$path = "../uploads/";
$path_driver= "../uploads/driver/";

$img_type = $_POST["img_type"];  // vehicle, driver


if( $img_type == "vehicle" ){

	$valid_formats = array("jpg", "png", "gif", "bmp", "JPG");
	if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
		{
			$filename = $_FILES['photoimg']['name'];
			$size = $_FILES['photoimg']['size'];
			
			if(strlen($filename))
				{
					if ( !is_dir("upload") ) {
						mkdir("upload");
					}
					
					list($txt, $ext) = explode(".", $filename);
					if(in_array($ext,$valid_formats))
					{
						if($size<(5024*5024))
							{
								//$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
								$actual_image_name = $txt.".".$ext;
								$tmp = $_FILES['photoimg']['tmp_name'];
								if(move_uploaded_file($tmp, $path.$actual_image_name))
									{
									//mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
										echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
										$newpath = 'uploads/';
										$newpaths = $newpath.$actual_image_name;
										$_SESSION['varname'] = $newpaths;
										
										$file = explode(".", $_FILES["photoimg"]["name"]);
										$type = getPictureType($file[1]);
										resize($path, $path, $_FILES["photoimg"]["name"], $type, 390,340);
										
									}
										
								else
									echo "<font color='red'>failed </font>";
							}
							else
							echo "<font color='red'> Image file size max 2 MB </font>";					
					}
						else
						echo "<font color='red'> Invalid file format.. </font>";	
				}
				
			else
				echo "<font color='red'>Please select image..! </font>";
				
			exit;
	
		}
}

else if( $img_type == "driver" ){

	$valid_formats = array("jpg", "png", "gif", "bmp", "JPG");
	if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
		{
			$filename = $_FILES['photoimg']['name'];
			$size = $_FILES['photoimg']['size'];
			
			if(strlen($filename))
				{
					if ( !is_dir("upload/driver") ) {
						mkdir("upload/driver");
					}
					
					list($txt, $ext) = explode(".", $filename);
					if(in_array($ext,$valid_formats))
					{
						if($size<(5024*5024))
							{
								//$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
								$actual_image_name = $txt.".".$ext;
								$tmp = $_FILES['photoimg']['tmp_name'];
								if(move_uploaded_file($tmp, $path_driver.$actual_image_name))
									{
									//mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
										echo "<img src='uploads/driver/".$actual_image_name."'  class='preview'>";
										$newpath = 'uploads/driver/';
										$newpaths = $newpath.$actual_image_name;
										$_SESSION['varname'] = $newpaths;
										
										$file = explode(".", $_FILES["photoimg"]["name"]);
										$type = getPictureType($file[1]);
										resize($path_driver, $path_driver, $_FILES["photoimg"]["name"], $type, 300,250);
									}
										
								else
									echo "<font color='red'>failed </font>";
							}
							else
							echo "<font color='red'> Image file size max 2 MB </font>";					
					}
						else
						echo "<font color='red'> Invalid file format.. </font>";	
				}
				
			else
				echo "<font color='red'>Please select image..! </font>";
				
			exit;
	
		}
}

?>